import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

let router = new VueRouter({
  base: "sumsung",
  mode: "hash",
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      name: "login",
      path: "/login",
      component: (resolve) => require(["@/views/user/login.vue"], resolve),
    },
    {
      name: "welcome",
      path: "/welcome",
      component: (resolve) => require(["@/views/edit/welcome.vue"], resolve),
      meta: {
        requireAuth: true,
      },
    },
    {
      name: "template",
      path: "/template",
      component: (resolve) => require(["@/views/edit/template.vue"], resolve),
      meta: {
        requireAuth: true,
      },
    },
    {
      name: "content",
      path: "/content",
      component: (resolve) => require(["@/views/edit/content.vue"], resolve),
      meta: {
        requireAuth: true,
      },
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.matched.some((res) => res.meta.requireAuth)) {
    // 验证是否需要登陆
    var id = window.localStorage.getItem("eplatformid");
    if (id) {
      // 查询本地存储信息是否已经登陆
      next();
    } else {
      next({
        path: "/login", // 未登录则跳转至login页面
      });
    }
  } else {
    next();
  }
});

export default router;
